import { createApp } from 'vue'
import { createPinia } from 'pinia'

import App from './App.vue'
import router from './router'

import { filterRoutes } from "./utils/authorization";
import  routes  from "./router/routes";

const app = createApp(App)

app.use(createPinia())

// 为了防止登陆后 刷新页面导致 路由丢失，需要在每次刷新时重新过滤用户权限访问的路由并动态添加路由到router中
if (JSON.parse(localStorage.getItem('userinfo'))) {
    // 获取本地存储中的用户信息中的权限列表
let permissions = JSON.parse(localStorage.getItem('userinfo')).role.permissions
let accessRoutes = filterRoutes(
    routes,
    permissions
);
accessRoutes.forEach((route) => {
    // 循环一次，添加一条路由
    router.addRoute(route)
});
} else {
    router.push('/login')
}
app.use(router)
app.mount('#app')
